<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryParams" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="主题" prop="crCmTitle">
          <el-input v-model="queryParams.crCmTitle" placeholder="请输入班会主题" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="班级" prop="crClassNo">
          <el-input v-model="queryParams.crClassNo" placeholder="班级" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="辅导员" prop="crTeacherNo">
          <el-input v-model="queryParams.crTeacherNo" placeholder="请输入辅导员" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="时间" prop="crTime">
          <el-date-picker clearable v-model="queryParams.crTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择班会时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="地址" prop="crAddr">
          <el-input v-model="queryParams.crAddr" placeholder="请输入班会地址" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
            v-hasPermi="['classmetting:classMettingContent:edit']">修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
            v-hasPermi="['classmetting:classMettingContent:remove']">删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
            v-hasPermi="['classmetting:classMettingContent:export']">导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="classMettingContentList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="${comment}" align="center" prop="crId" v-if="false" />
        <el-table-column label="材料编码" align="center" prop="crNo" v-if="false" />
        <el-table-column label="班会主题" align="center" prop="crCmTitle" :show-overflow-tooltip="true" />
        <el-table-column label="班级名称" align="center" prop="className" />
        <el-table-column label="辅导员" align="center" prop="crTeacherName" />
        <el-table-column label="班会总结" align="center" prop="crTextContent" :show-overflow-tooltip="true" />
        <el-table-column label="班会时间" align="center" prop="crTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.crTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="班会地址" align="center" prop="crAddr" />
        <el-table-column label="提交时间" align="center" prop="createTime" width="180px"/>
        <el-table-column label="状态" align="center" prop="status" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
  
      <!-- 添加或修改班会内容对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="班会标题" prop="crCmTitle">
            <el-input v-model="form.crCmTitle" disabled />
          </el-form-item>
          <el-form-item label="班会班级" prop="crClassNo">
            <el-select v-if="isEmpty(form.crId)" multiple style="width: 100%;" v-model="crClassNoList">
              <el-option v-for="(v, i) in classList" :key="i" :label="v.className"
                :value="v.classId.toString()"></el-option>
            </el-select>
            <el-select v-else style="width: 100%;" v-model="form.crClassNo">
              <el-option v-for="(v, i) in classList" :key="i" :label="v.className"
                :value="v.classId.toString()"></el-option>
            </el-select>
            <!-- <el-cascader placeholder="请选择班级" v-model="crClassNoList" :show-all-levels="false" :options="ClassNameList"
              @change="handleChange1" clearable filterable>
              <template slot-scope="{ node, data }">
                <span>{{ data.label }}</span>
                <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
              </template>
            </el-cascader> -->
          </el-form-item>
          <el-form-item label="班会总结">
            <editor v-model="form.crContent" ref="uedit" :min-height="192" />
          </el-form-item>
          <el-form-item label="班会时间" prop="crTime">
            <el-date-picker clearable v-model="form.crTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择班会时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="班会地址" prop="crAddr">
            <el-input v-model="form.crAddr" placeholder="请输入班会教室或者具体地址" />
          </el-form-item>
          <el-form-item>
            <AttachUpload :fileListPut="tmpResList" v-model="form.attachUrls" :fileType="fileType"></AttachUpload>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { listFdy as listClassMettingContent, getClassMettingContent, delClassMettingContent, addClassMettingContent, updateClassMettingContent } from "@/api/routine/classmeting/classMettingContent";
  import { listClassMettingTheme, listClassByTNo } from "@/api/routine/theme/ClassMettingTheme";
  import { getClassName } from "@/api/stuCQS/basedata/student";
  import AttachUpload from "@/components/AttachUpload"
  import { fullLoading, isEmpty } from "@/api/helpFunc";
  
  
  export default {
    name: "fdy",
    components: { AttachUpload },
    data() {
      return {
        isEmpty,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        //主题编码
        cmNo: "",
        //班级id
        crClassNoList: [],
        // 总条数
        total: 0,
        // 班会内容表格数据
        classMettingContentList: [],
        //附件临时列表
        tmpResList: [],
        // 弹出层标题
        title: "",
        //班级列表
        ClassNameList: [],
        fileType: [ ".png", ".jpg", ".jpeg"],
        // 是否显示弹出层
        open: false,
        //班会主题内容
        classTheme: {},
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          crNo: null,
          crCmNo: null,
          crClassNo: null,
          crTeacherNo: null,
          crContent: null,
          crTime: null,
          crAddr: null,
          status: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        },
  
        classList: [],
  
        crClassNoList: []
      };
    },
    created() {
      this.listClassByTNo();
      this.cmNo = this.$store.cmNo;
      this.getList();
      this.getClassNameList();
      this.getClassTheme();
    },
    methods: {
      async listClassByTNo() {
        let loading = fullLoading(this);
        let res = await listClassByTNo();
        loading.close();
        if (res.code == 200) {
          this.classList = [...res.data];
          console.log(this.classList);
        }
      },
      /** 查询班会内容列表 */
      getList() {
        this.loading = true;
        this.queryParams.crCmNo = this.cmNo;
        listClassMettingContent(this.queryParams).then(response => {
          this.classMettingContentList = response.rows;
          this.total = response.total;
          this.loading = false;
          this.resetForm("queryForm");
        });
      },
      //查询班会主题内容
      getClassTheme() {
        var queryTheme = {};
        queryTheme.cmNo = this.cmNo;
        listClassMettingTheme(queryTheme).then(res => {
          if (res.total > 0) {
            this.classTheme = res.rows[0];
          }
        });
  
      },
      //查询班级
      getClassNameList() {
        getClassName().then(res => {
          this.ClassNameList = res.data
        })
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.crClassNoList = [];
        this.form = {
          crId: null,
          crNo: null,
          crCmNo: null,
          crClassNo: null,
          crDepartmentNo: null,
          crMajorNo: null,
          crTeacherNo: null,
          crContent: null,
          crTextContent: null,
          attachResourceList: null,
          crTime: null,
          crAddr: null,
          status: null,
          delFlag: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      //班级
      handleChange1(value) {
        this.form.crDepartmentNo = value[0];
        this.form.crMajorNo = value[1];
        this.form.crClassNo = value[2];
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.crId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.crClassNoList = [];
        this.reset();
        this.form.crCmTitle = this.classTheme.cmTitle;
        this.open = true;
        this.title = "添加班会内容";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const crId = row.crId || this.ids
        getClassMettingContent(crId).then(response => {
          console.log(response);
          this.form = response.data;
          let tmpurl = [];
          tmpurl = this.form.attachResourceList;
          this.tmpResList = [];
          //this.form.attachUrls="";  
          tmpurl.forEach(item => {
            this.tmpResList.push({
              originalFileName: item.resName,
              url: item.resPath,
              name: item.resName
            });
          });
          this.open = true;
          this.crClassNoList = [Number(this.form.crDepartmentNo), Number(this.form.crMajorNo), Number(this.form.crClassNo)];  //赋值班级
          console.log(this.crClassNoList);
          this.title = "修改班会内容";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            let attachamentFileTemp = [];
            this.tmpResList.forEach(element => {
              attachamentFileTemp.push({
                resName: element.originalFileName,
                resPath: element.url
              });
            });
            this.form.attachResourceList = attachamentFileTemp;
            this.form.crTextContent = this.$refs.uedit.getContent();
            if (this.form.crId != null) {
              updateClassMettingContent(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              if (this.crClassNoList.length != 0) {
                let loading = fullLoading(this);
                this.crClassNoList.map(x => {
                  setTimeout(() => {
                    let sdata = { ...this.form };
                    sdata.crCmNo = this.cmNo;
                    sdata.crClassNo = x;
                    addClassMettingContent(sdata).then(response => {
                      this.$modal.msgSuccess("新增成功");
                      this.open = false;
                      this.getList();
                    });
                  }, 1000);
                });
                loading.close();
              } else {
                this.$message.info("请选择班级");
              }
  
  
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const crIds = row.crId || this.ids;
        this.$modal.confirm('是否确认删除班会内容编号为"' + crIds + '"的数据项？').then(function () {
          return delClassMettingContent(crIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => { });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('classmetting/classMettingContent/export', {
          ...this.queryParams
        }, `classMettingContent_${new Date().getTime()}.xlsx`)
      }
    }
  };
  </script>
  